<?php

/**
 * 第三方登陆
 *
 * @author andery
 */
class oauth {

    private $_type = '';
    private $_setting = array();

    public function __construct($type) {
        $this->_type = $type;
        //加载登陆接口配置
        $this->_setting =array('app_key'=>'101260783','app_secret'=>'184a7a04d958c3d71c19956f005b35cb');
        //导入接口文件
        include_once LIB_PATH . 'Zyblib/oauth/' . $type . '/' . $type . '.php';
        $om_class = $type . '_oauth';
        $this->_om = new $om_class($this->_setting);
    }

    /**
     * 跳转到授权页面
     */
    public function authorize() {
        redirect($this->_om->getAuthorizeURL());
    }

    /**
     * 登陆回调
     */
    public function callbackLogin($request_args) {
        $user = $this->_om->getUserInfo($request_args);
        $users=M('user')->where(array('keyid'=>$user['keyid']))->find();
        if(!$user['keyid']){
        	exit();
          }
        if ($users) {
            //已经绑定过则更新绑定信息 自动登陆
            $user_info = M('user')->field('id,username')->where(array('id' =>$users['id']))->find();
            //登陆
            $this->_oauth_visitor()->assign_info($user_info);
            return U('user/user/index');
        } else {
            //处理用户名
            if (M('user')->where(array('username' => $user['keyname']))->count()) {
                $user['username'] = $user['keyname'] . '_' . mt_rand(99, 9999);
            } else {
                $user['username'] = $user['keyname'];
            }
            $uid=M('user')->add(
               array(
               'username'=>$user['username'],
               'password'=>md5('11ddddddddd'),
               'status'=>1,
               'keyid'=>$user['keyid'],
              ));
              if($uid){

		     	saveScore('reg',$uid,$user['username'],'注册赠送利币');
		     	saveJuan('reg',$uid,$user['username'],'注册赠送优惠码');
	            $userdata['uid']=$uid;
				$userdata['reg_ip']=get_client_ip();
				$userdata['reg_time']=time();
				$rs=M('user_data')->add($userdata);
              }


//            if ($user['keyavatar_big']) {
//                //下载原始头像到本地临时储存  用日期文件夹分类  方便清理
//                $avatar_size = explode(',', C('zyb_avatar_size'));
//                $size = in_array($size, $avatar_size) ? $size : '160';
//                $image_content = Http::fsockopenDownload($user['keyavatar_big']);
//                file_put_contents( C('zyb_attach_path') . 'avatar/' . avatar_dir($uid).md5($uid).'_'.$size.'.jpg', $image_content);
//            }
            //把第三方的数据存到COOKIE
            $this->_oauth_visitor()->assign_info(array('id'=>$uid,'uesrname'=>$user['username']));
            return U('user/user/index');
        }
    }

    /**
     * 访问者
     */
    private function _oauth_visitor() {
        include_once ( LIB_PATH.'Zyblib/user_visitor.class.php');
        return new user_visitor();
    }

    /**
     * 返回需要的参数
     */
    public function NeedRequest() {
        return $this->_om->NeedRequest();
    }

}